using System;
using System.Globalization;
using System.IO;
using Newtonsoft.Json.Linq;

namespace Megaplan.API.Extentions
{
    /// <summary>
    /// Extension method overload!
    /// </summary>
    public static class MiscExtensions
    {
        public static bool HasValue(this string input)
        {
            return !string.IsNullOrEmpty(input);
        }

        /// <summary>
        /// Read a stream into a byte array
        /// </summary>
        /// <param name="input">Stream to read</param>
        /// <returns>byte[]</returns>
        public static byte[] ReadAsBytes(this Stream input)
        {
            var buffer = new byte[16*1024];
            using (var ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        /// <summary>
        /// Copies bytes from one stream to another
        /// </summary>
        /// <param name="input">The input stream.</param>
        /// <param name="output">The output stream.</param>
        public static void CopyTo(this Stream input, Stream output)
        {
            var buffer = new byte[32768];
            while (true)
            {
                int read = input.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return;
                output.Write(buffer, 0, read);
            }
        }

        /// <summary>
        /// Gets string value from JToken
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string AsString(this JToken token)
        {
            return token.Type == JTokenType.String ? token.Value<string>() : token.ToString();
        }

        /// <summary>
        /// Gets string value from JToken
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string AsString(this JToken token, CultureInfo culture)
        {
            var value = token as JValue;
            if (value != null)
            {
                return (string) Convert.ChangeType(value.Value, typeof (string), culture);
            }

            return token.Type == JTokenType.String ? token.Value<string>() : token.ToString();
        }

//        /// <summary>
//        /// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
//        /// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
//        /// </summary>
//        /// <param name="buffer">An array of bytes to convert</param>
//        /// <returns>The byte as a string.</returns>
//        public static string AsString(this byte[] buffer)
//        {
//            if (buffer == null || buffer.Length == 0)
//                return "";

//            // Ansi as default
//            Encoding encoding = Encoding.UTF8;

//            /*
//                EF BB BF		UTF-8 
//                FF FE UTF-16	little endian 
//                FE FF UTF-16	big endian 
//                FF FE 00 00		UTF-32, little endian 
//                00 00 FE FF		UTF-32, big-endian 
//                */

//            if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
//            {
//                encoding = Encoding.UTF8;
//            }
//            else if (buffer[0] == 0xfe && buffer[1] == 0xff)
//            {
//                encoding = Encoding.Unicode;
//            }
//            else if (buffer[0] == 0xfe && buffer[1] == 0xff)
//            {
//                encoding = Encoding.BigEndianUnicode; // utf-16be
//            }
//#if FRAMEWORK
//            else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
//            {
//                encoding = Encoding.UTF32;
//            }
//            else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
//            {
//                encoding = Encoding.UTF7;
//            }
//#endif
//            using (var stream = new MemoryStream())
//            {
//                stream.Write(buffer, 0, buffer.Length);
//                stream.Seek(0, SeekOrigin.Begin);
//                using (var reader = new StreamReader(stream, encoding))
//                {
//                    return reader.ReadToEnd();
//                }
//            }
//        }
    }
}